package cn.itheima.student.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import cn.itheima.student.daomain.Student;
import cn.itheima.student.service.StudentService;
import cn.itheima.student.service.impl.StudentServiceImpl;


public class StudentAddServlet1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
  
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	doGet(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		/*String sname = request.getParameter("sname");
		String sex = request.getParameter("sex");
		String email = request.getParameter("eamil");
		String phone = request.getParameter("telephone");
		String addr = request.getParameter("addr");
		String age = request.getParameter("age");
		Student stu = new Student();
		stu.setAddr(addr);
		stu.setAge(Integer.parseInt(age));
		stu.setEmail(email);
		-------------------
		*/
		
		//获取用户提交的所有参数，key：对应<input name = "sname"/> value : 用户输入的值
		Map<String, String[]> map = request.getParameterMap();
		//2.一键封装
		Student stu = new Student();
		try {
			BeanUtils.populate(stu, map);
			
			//调用service 完成添加
			StudentService service = new StudentServiceImpl();
			service.add(stu);
			response.sendRedirect(request.getContextPath()+"/StudentFindAllServlet1");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}

}
